package com.banker.web;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.banker.entity.ToAssign;
import com.banker.service.ToAssignService;
import com.banker.web.query.ToAssignQuery;
import com.ibeetl.admin.core.annotation.Function;
import com.ibeetl.admin.core.file.FileService;
import com.ibeetl.admin.core.util.ConvertUtil;
import com.ibeetl.admin.core.util.ValidateConfig;
import com.ibeetl.admin.core.web.JsonResult;

/**
 * bank 接口
 */
@Controller
public class ToAssignController{

	private final Log log = LogFactory.getLog(this.getClass());
	private static final String MODEL = "/to_assign/bank";


	@Autowired private ToAssignService bankService;

	@Autowired
	FileService fileService;
	/* 页面 */

	@GetMapping(MODEL + "/index.do")
	@Function("to_assign.bank.query")
	@ResponseBody
	public ModelAndView index() {
		ModelAndView view = new ModelAndView("/to_assign/bank/index.html") ;
		view.addObject("search", ToAssignQuery.class.getName());
		return view;
	}

	@GetMapping(MODEL + "/edit.do")
	@Function("to_assign.bank.edit")
	@ResponseBody
	public ModelAndView edit(Integer id) {
		ModelAndView view = new ModelAndView("/to_assign/bank/edit.html");
		ToAssign bank = this.bankService.queryById(id);
		view.addObject("bank", bank);
		return view;
	}

	@GetMapping(MODEL + "/add.do")
	@Function("to_assign.bank.add")
	@ResponseBody
	public ModelAndView add() {
		ModelAndView view = new ModelAndView("/to_assign/bank/add.html");
		return view;
	}

	/* ajax json */

	@PostMapping(MODEL + "/list.json")
	@Function("to_assign.bank.query")
	@ResponseBody
	public JsonResult<PageQuery> list(ToAssignQuery condtion)
	{
		PageQuery page = condtion.getPageQuery();
		this.bankService.queryByCondition(page);
		return JsonResult.success(page);
	}

	@PostMapping(MODEL + "/add.json")
	@Function("to_assign.bank.add")
	@ResponseBody
	public JsonResult add(@Validated(ValidateConfig.ADD.class)ToAssign bank)
	{
		this.bankService.save(bank);
		new JsonResult();
		return JsonResult.success();
	}

	@PostMapping(MODEL + "/edit.json")
	@Function("to_assign.bank.edit")
	@ResponseBody
	public JsonResult<String> update(@Validated(ValidateConfig.UPDATE.class)  ToAssign bank) {
		boolean success = this.bankService.update(bank);
		if (success) {
			new JsonResult();
			return JsonResult.success();
		} else {
			return JsonResult.failMessage("保存失败");
		}
	}



	@GetMapping(MODEL + "/view.json")
	@Function("to_assign.bank.query")
	@ResponseBody
	public JsonResult<ToAssign>queryInfo(Integer id) {
		ToAssign bank = this.bankService.queryById( id);
		return  JsonResult.success(bank);
	}

	@PostMapping(MODEL + "/delete.json")
	@Function("to_assign.bank.delete")
	@ResponseBody
	public JsonResult delete(String ids) {
		if (ids.endsWith(",")) {
			ids = StringUtils.substringBeforeLast(ids, ",");
		}
		List<Long> idList = ConvertUtil.str2longs(ids);
		this.bankService.batchDelToAssign(idList);
		new JsonResult();
		return JsonResult.success();
	}


}
